//
//  GMExaminationVC.m
//  Teecloud
//
//  Created by 小熊 on 2018/10/24.
//  Copyright © 2018年 小志. All rights reserved.
//

#import "GMExaminationVC.h"
#import "MyWKWebView.h"
#import "GMExamModel.h"
#import "ZJTimeCountDown.h"
#import "GMExamVC.h"
#import "GMSubjectsModel.h"
@interface GMExaminationVC ()
@property (weak, nonatomic) IBOutlet UILabel *titleName;

@property (weak, nonatomic) IBOutlet MyWKWebView *webView;
@property (weak, nonatomic) IBOutlet UILabel *begainLab;
@property (weak, nonatomic) IBOutlet UILabel *begainTime;
@property (weak, nonatomic) IBOutlet UILabel *delayedTime;
/**考试未完成显示*/
@property (weak, nonatomic) IBOutlet UILabel *baseLab;
@property (weak, nonatomic) IBOutlet UIButton *baseBtn;
@property (nonatomic, strong) GMExamModel *baseModel;
@end

@implementation GMExaminationVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"在线考试";
    if (kIsEmptyObject(self.fromType)) {
        self.fromType = @(1);
    }
    [self getExaminationData];
    [self addLeftBarButtonWithImage:[UIImage imageNamed:@"left-white-64"] action:@selector(left_BarButtonItemAction)];
}
- (void)left_BarButtonItemAction {
    if (kIsEmptyString(self.typeStr)) {
        [self.navigationController popViewControllerAnimated:YES];
    }else{
        [self.navigationController popToRootViewControllerAnimated:YES];
    }
}

-(void)getExaminationData{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setValue:FROM_IOS forKey:@"from"];
    [dict setValue:[XGMArchiverTools cacheForKey:BUSIORGID] forKey:@"busiOrgId"];
    [dict setValue:[XGMArchiverTools cacheForKey:USER_UserId] forKey:@"userId"];
    [dict setValue:[XGMArchiverTools cacheForKey:USER_identifierNumber] forKey:@"loginFinger"];
    [dict setValue:self.groupId forKey:@"groupId"];
    [dict setValue:self.resId forKey:@"resId"];
    [dict setValue:self.fromType forKey:@"fromType"];
    [dict setValue:self.packId forKey:@"packId"];
    [dict setValue:self.TestRoomId forKey:@"testRoomId"];
    [dict setValue:self.TestTimeId forKey:@"testTimeId"];
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [HJNetwork HTTPWithMethod:HJRequestMethodPOST url:[NSString stringWithFormat:@"ajax-api.ashx?$c=TeeyunApp&$m=GetTestInfo&$v=%@",VERSION] parameters:dict cachePolicy:HJCachePolicyIgnoreCache callback:^(id responseObject, NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if (!error) {
            if ([[responseObject objectForKey:RESULT_NAME_Success] boolValue]) {
                GMExamModel *model = [GMExamModel mj_objectWithKeyValues:[responseObject objectForKey:RESULT_NAME_Data]];
                for (GMSubjectsModel *subModel in model.Subjects) {
                    subModel.examId = model.Id;
                }
                self.baseModel = model;
                [self updataInfoWithModel:model];
            }else{
                UIAlertController *alert  = [UIAlertController zj_alertControllerWithTitle:@"提示" message:[responseObject objectForKey:RESULT_NAME_Message] optionStyle:OptionStyleStyleOnlyOK OkTitle:@"好的" cancelTitle:@"" okBlock:^{
                    [self.navigationController popViewControllerAnimated:YES];
                } cancelBlock:^{
                    [self.navigationController popViewControllerAnimated:YES];
                }];
                [self presentViewController:alert animated:YES completion:nil];
            }
        }else{
            [MBProgressHUD showErrorOrSuc:@"请求出错" toView:self.view];
        }
    }];
}
-(void)updataInfoWithModel:(GMExamModel *)model{
    self.titleName.text = model.Title;
    self.baseLab.hidden = YES;
    if (kIsEmptyString(model.Id)) {
        self.webView.hidden = NO;
        self.baseLab.hidden = YES;
        [self.webView loadHTMLString:model.TestNotes baseURL:nil];
        kWeakObject(self)
        if (model.IsStarted) {
            if (model.DelayTime >0) {
                [[ZJTimeCountDown alloc] zj_timeCountDownWithSecondTime:model.DelayTime completeBlock:^(NSInteger hour, NSInteger minute, NSInteger second) {
                    NSString *temStr = [NSString stringWithFormat:@"%02ld:%02ld:%02ld",(long)hour,(long)minute,(long)second];
                    [weakself.baseBtn setTitle:[NSString stringWithFormat:@"防拥堵延时:%@",temStr] forState:UIControlStateNormal];
                }];
            }else{
                 [self.baseBtn setTitle:@"开始答题" forState:UIControlStateNormal];
            }
        }else{
            [[ZJTimeCountDown alloc] zj_timeCountDownWithSecondTime:model.SpaceTime completeBlock:^(NSInteger hour, NSInteger minute, NSInteger second) {
                NSString *temStr = [NSString stringWithFormat:@"%02ld:%02ld:%02ld",(long)hour,(long)minute,(long)second];
                [weakself.baseBtn setTitle:[NSString stringWithFormat:@"距离开考:%@",temStr] forState:UIControlStateNormal];
            }];
        }

    }else{
        //Id!=null：表示有未完成考试，此时考试须知部分变为一段警示文字，“开始答题”变为“继续答题”
        self.webView.hidden = YES;
        self.baseLab.hidden = NO;
        [self.baseBtn setTitle:@"继续答题" forState:UIControlStateNormal];
    }
   
    
}
/**开始答题*/
- (IBAction)begExaminnation:(UIButton *)sender {
    if ([sender.titleLabel.text isEqualToString:@"开始答题"]) {
        GMExamVC *vc = [[GMExamVC alloc] init];
        vc.resId = self.resId;
        vc.TestRoomId = self.TestRoomId;
        vc.TestTimeId = self.TestTimeId;
        [self.navigationController pushViewController:vc animated:YES];
    }else if([sender.titleLabel.text isEqualToString:@"继续答题"]){
        GMExamVC *vc = [[GMExamVC alloc] init];
        vc.baseModel = self.baseModel;
        [self.navigationController pushViewController:vc animated:YES];
    }
}

-(void)dealloc{
    [[ZJTimeCountDown alloc] zj_timeDestoryTimer];
}

@end
